<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2019/12/17
// +----------------------------------------------------------------------

namespace app\index\controller\api;

use app\common\constants\ApiConstant;
use think\App;
use think\Db;

/**
 * 新闻中心
 * Class NewsCenter
 * @package app\index\controller\api
 * @author feraner<feraner@163.com>
 * @date 2019/12/17
 */
class NewsCenter extends ApiBase
{

    public function __construct(App $app = null)
    {
        parent::__construct($app);
    }


    /**
     * 资讯列表 -- 某个分类下的资讯列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function newsList()
    {
        $typeId = $this->request->param('typeId', 0);
        $page = $this->request->param('page', 1);
        $pageSize = $this->request->param('pageSize', 8);
        if ($typeId) {
            $typeInfo = Db::name('web_news_type')
                ->where('id', $typeId)
                ->find() ?: [];
            if ($typeInfo) {
                $newsList = Db::name('web_news')
                    ->where('tid', $typeId)
                    ->where('status', 1)
                    ->order('top DESC,sort ASC,click DESC')
                    ->page($page, $pageSize)
                    ->select() ?: [];
                $this->response([
                    'newsList'          => $newsList,
                    'typeInfo'          => $typeInfo
                ]);
            }
        }
        $this->response([], ApiConstant::API_PARAMETER_ERROR, ApiConstant::API_PARAMETER_ERROR);
    }




    /**
     * 帮助中心--新闻列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function helpList()
    {
        $helpNewsList = Db::name('web_news')->alias('wn')
            ->join('web_news_type wnt', 'wn.tid = wnt.id')
            ->where('wnt.flag', 'new_user')
            ->where('wn.status', 1)
            ->order('wn.sort', 'ASC')
            ->field('wn.*')
            ->limit(0, 8)
            ->select() ?: [];


        $this->response($helpNewsList);

    }


    /**
     * 资讯详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function newsDetail()
    {
        $newsId = $this->request->param('id', 0);
        if ($newsId) {
            $newsInfo = Db::name('web_news')
                ->where('id', $newsId)
                ->where('status', 1)
                ->find() ?: [];
            if ($newsInfo) {
                $this->response($newsInfo);
            }
            $this->response([], ApiConstant::API_ERROR, ApiConstant::API_ERROR);
        }
        $this->response([], ApiConstant::API_PARAMETER_ERROR, ApiConstant::API_PARAMETER_ERROR);
    }


    /**
     * 单页信息
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function singlePage()
    {
        $pageFlag = $this->request->param('pageFlag', '');
        if ($pageFlag) {
            $pageInfo = Db::name('web_about')
                ->where('flag', $pageFlag)
                ->find() ?: [];
            $this->response($pageInfo);
        }
        $this->response([], ApiConstant::API_PARAMETER_ERROR, ApiConstant::API_PARAMETER_ERROR);
    }











}
